# coding:utf-8
from regressor.regressor import Regressor
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures


class PolynomialRegressor(Regressor):
    # 阶数
    degree = 2
    
    def __init__(self):
        Regressor.__init__(self)
        self.algorithm_name = "多项式回归"
        self.ipynb_template_name = "polynomial_regressor-template.ipynb"
        
    def implent(self): 
        Regressor.implent(self)
        # 构造模型
        polynomial_features = PolynomialFeatures(degree=self.degree, include_bias=False)
        linear_regression = LinearRegression()
        self.algorithm = Pipeline([("PolynomialFeatures", polynomial_features), ("LinearRegression", linear_regression)])
        # 训练模型
        self.algorithm.fit(self.train_inputs, self.train_outputs)
        # 评估模型
        self.train_score = self.algorithm.score(self.train_inputs, self.train_outputs)
        self.test_score = self.algorithm.score(self.test_inputs, self.test_outputs)
        # 预测
        self.predict_output_values = self.algorithm.predict(self.predict_input_values)
    
    def prepareIpynbItems(self):
        Regressor.prepareIpynbItems(self)
        self.ipynb_items["#degree#"] = self.degree
